package edu.cmsc611.textsimulator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Formatter;

public class OutputChart {
	private static OutputChart instance=null;
	private String chart[][];
	private int instructionsize;
	public void setvalue(int r, int c,String value)
	{
		this.chart[r][c]=value;
	}
	public String getValueAt(int r, int c)
	{
		return this.chart[r][c];
	}
	public void createChart(int instructionsize)
	{
		this.instructionsize=instructionsize;
		//every instruction should have 8 value at most, 4 for stages and 4 for hazards
		this.chart=new String[instructionsize+1][9];
		this.chart[0][0]="Instruction";
		this.chart[0][1]="FT";
		this.chart[0][2]="ID";
		this.chart[0][3]="EX";
		this.chart[0][4]="WB";
		this.chart[0][5]="RAW";
		this.chart[0][6]="WAR";
		this.chart[0][7]="WAW";
		this.chart[0][8]="Struct";

		for(int i=1;i<=instructionsize;i++)
			for(int j=0;j<9;j++)
			{
				this.chart[i][j]="";
				if(j>4)
					this.chart[i][j]="N";
			}
		//return this.chart;
	}
	protected OutputChart(){

	}
	public void increasesizefornextiteration(int iteration)
	{
		int rowsize=(iteration+1)*this.instructionsize;
		String tempchart[][]=new String[rowsize+1][9];
		for(int i=1;i<=rowsize;i++)
			for(int j=0;j<9;j++)
			{
				tempchart[i][j]="";
				if(j>4)
					tempchart[i][j]="N";
			}


		for(int i=1;i<=this.instructionsize;i++)
			for(int j=0;j<9;j++)
				tempchart[i][j]=this.chart[i][j];
		this.chart=tempchart;
		this.chart[0][0]="Instruction";
		this.chart[0][1]="FT";
		this.chart[0][2]="ID";
		this.chart[0][3]="EX";
		this.chart[0][4]="WB";
		this.chart[0][5]="RAW";
		this.chart[0][6]="WAR";
		this.chart[0][7]="WAW";
		this.chart[0][8]="Struct";
	}
	public void printChart(boolean console,boolean file)
	{
		FileManage files=FileManage.getInstance();
		if(console){
			System.out.println();
			for(String[] row : chart) {
				printRow(row);
			}
		}
		if(file)
		{
			File resultFile = new File(files.getResutlFile());

			String output;
			try {
				//output = new Formatter(resultFile);
				// if file doesnt exists, then create it
				if (!resultFile.exists()) {
					try {
						resultFile.createNewFile();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

                LRUCache lrucache=LRUCache.getInstance();
				FileWriter fw = new FileWriter(resultFile.getAbsoluteFile());
				BufferedWriter bw = new BufferedWriter(fw);
				int colnum=0;
				String []outputline=new String[9];
				for(String[] row : chart) {
					for (String i : row) {
						if(colnum==0)
						{
							outputline[colnum]=i;
							//bw.write(String.format("%25s", i));
						}
						//System.out.format("%20s", i);
						else{
							outputline[colnum]=i;
							//bw.write(String.format("%-6s", i));
						}
						//System.out.print(i);
						//System.out.print("\t\t\t");
						
						colnum++;
					}
					bw.write(String.format("%20s%8s%8s%8s%8s%8s%8s%8s%8s", outputline[0], outputline[1], outputline[2], outputline[3], outputline[4], outputline[5], outputline[6], outputline[7], outputline[8]));
					bw.newLine();
					colnum=0;
				}
				bw.newLine();
				bw.newLine();
				
				
				bw.write("Total number of requests to instruction cache  "+lrucache.getInsRequested());
				bw.newLine();
				bw.write("Total number of instruction cache hit  "+lrucache.getInsCacheHitOccures());
				bw.newLine();
				
				
				bw.write("Total number of requests to data cache  "+lrucache.getDataRequested());
				bw.newLine();
				bw.write("Total number of data cache hit  "+lrucache.getDataHitOccures());

				
				bw.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}







			System.out.println("Done");
		}
		//		for(int r=0;r<chart.length;r++){
		//			for(int c=0;c<9;c++)
		//				System.out.print(chart[r][c]);
		//			System.out.println();
		//		}
	}

	public void printRow(String[] row) {
		int colnum=0;
		for (String i : row) {
			if(colnum==0)
				System.out.format("%20s", i);
			else
				System.out.format("%10s", i);
			//System.out.print(i);
			//System.out.print("\t\t\t");

			colnum++;
		}
		System.out.println();
	}


	public static OutputChart getInstance(){
		if(instance==null){
			instance=new OutputChart();
		}
		return instance;
	}
}
